package com.qf.kdgroup.gdbackend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.kdgroup.gdbackend.dto.ResultBean;
import com.qf.kdgroup.gdbackend.entity.Device;
import com.qf.kdgroup.gdbackend.mapper.DeviceMapper;
import com.qf.kdgroup.gdbackend.service.DeviceService;
import com.qf.kdgroup.gdbackend.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public PageInfo getDevicesList(PageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum().intValue(), pageVO.getPageSize().intValue());
        List<Device> tbProducts = deviceMapper.selectList(pageVO.getWid(),pageVO.getKeyword());
        PageInfo page = new PageInfo(tbProducts);
        return page;
    }

    @Override
    public ResultBean addDevice(Device device) {
        int res = deviceMapper.insertSelective(device);
        return res>0?new ResultBean().success():new ResultBean().error();
    }

    @Override
    public Device getDevice(Long did) {
        return deviceMapper.selectByPrimaryKey(did);
    }

    @Override
    public ResultBean updateDevice(Device device) {
        int res = deviceMapper.updateByPrimaryKeySelective(device);
        return res>0?new ResultBean().success():new ResultBean().error();
    }

    @Override
    public ResultBean deleteDevice(Long did) {
        Device device = deviceMapper.selectByPrimaryKey(did);
        if(device == null){
            return new ResultBean().error("未查询到该设备");
        }
        if(device.getStatus()==1){
            return new ResultBean().error("该设备处于开启状态，无法删除");
        }
        int res = deviceMapper.deleteByPrimaryKey(did);
        return res>0?new ResultBean().success("删除成功"):new ResultBean().error("删除失败");
    }



}
